package com.ruoyi.order.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.TbVoucherOrderDto;
import com.ruoyi.order.mapper.TbVoucherOrderMapper;
import com.ruoyi.order.domain.TbVoucherOrder;
import com.ruoyi.order.service.ITbVoucherOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 优惠券订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-09
 */
@Service
public class TbVoucherOrderServiceImpl implements ITbVoucherOrderService 
{
    @Autowired
    private TbVoucherOrderMapper tbVoucherOrderMapper;

    /**
     * 查询优惠券订单
     * 
     * @param id 优惠券订单主键
     * @return 优惠券订单
     */
    @Override
    public TbVoucherOrder selectTbVoucherOrderById(Long id)
    {
        return tbVoucherOrderMapper.selectTbVoucherOrderById(id);
    }

    /**
     * 查询优惠券订单列表
     * 
     * @param tbVoucherOrder 优惠券订单
     * @return 优惠券订单
     */
    @Override
    public List<TbVoucherOrderDto> selectTbVoucherOrderList(TbVoucherOrder tbVoucherOrder)
    {
        List<TbVoucherOrderDto> newlist = new ArrayList<>();
        List<TbVoucherOrder> list = tbVoucherOrderMapper.selectTbVoucherOrderList(tbVoucherOrder);
        for(TbVoucherOrder tbVoucherOrderOne:list){
            TbVoucherOrderDto tbVoucherOrderDto = new TbVoucherOrderDto();
            String userId= tbVoucherOrderOne.getUserId();
            String voucherId= tbVoucherOrderOne.getVoucherId();
            String userName = tbVoucherOrderMapper.selectTbUserNameById(userId);
            String voucherName = tbVoucherOrderMapper.selectTbVoucherNameById(voucherId);
            tbVoucherOrderDto.setUserName(userName);
            tbVoucherOrderDto.setVoucherName(voucherName);
            tbVoucherOrderDto.setId(tbVoucherOrderOne.getId());
            tbVoucherOrderDto.setUserId(tbVoucherOrderOne.getUserId());
            tbVoucherOrderDto.setVoucherId(tbVoucherOrderOne.getVoucherId());
            tbVoucherOrderDto.setPayType(tbVoucherOrderOne.getPayType());
            tbVoucherOrderDto.setStatus(tbVoucherOrderOne.getStatus());
            tbVoucherOrderDto.setUseTime(tbVoucherOrderOne.getUseTime());
            tbVoucherOrderDto.setPayTime(tbVoucherOrderOne.getRefundTime());
            tbVoucherOrderDto.setRefundTime(tbVoucherOrderOne.getPayTime());
            tbVoucherOrderDto.setUpdateTime(tbVoucherOrderOne.getUpdateTime());
            newlist.add(tbVoucherOrderDto);
        }
        return newlist;
    }

    /**
     * 新增优惠券订单
     * 
     * @param tbVoucherOrder 优惠券订单
     * @return 结果
     */
    @Override
    public int insertTbVoucherOrder(TbVoucherOrder tbVoucherOrder)
    {
        tbVoucherOrder.setCreateTime(DateUtils.getNowDate());
        return tbVoucherOrderMapper.insertTbVoucherOrder(tbVoucherOrder);
    }

    /**
     * 修改优惠券订单
     * 
     * @param tbVoucherOrder 优惠券订单
     * @return 结果
     */
    @Override
    public int updateTbVoucherOrder(TbVoucherOrder tbVoucherOrder)
    {
        tbVoucherOrder.setUpdateTime(DateUtils.getNowDate());
        return tbVoucherOrderMapper.updateTbVoucherOrder(tbVoucherOrder);
    }

    /**
     * 批量删除优惠券订单
     * 
     * @param ids 需要删除的优惠券订单主键
     * @return 结果
     */
    @Override
    public int deleteTbVoucherOrderByIds(Long[] ids)
    {
        return tbVoucherOrderMapper.deleteTbVoucherOrderByIds(ids);
    }

    /**
     * 删除优惠券订单信息
     * 
     * @param id 优惠券订单主键
     * @return 结果
     */
    @Override
    public int deleteTbVoucherOrderById(Long id)
    {
        return tbVoucherOrderMapper.deleteTbVoucherOrderById(id);
    }
}
